#!/bin/bash
IPFILE="$1"
SSH_PORT="$2"
SSH_PASSWORD="$3"

if ! command -v sshpass > /dev/null; then
  echo "sshpass is not installed! Please run: yum -y install sshpass."
  exit 1
fi

if [[ $# -eq 0 ]]; then
  echo "Usage: $0 file ssh_port password"
  exit 1
fi

if [[ ! -f "${IPFILE}" ]]; then
  echo "Error: File ${IPFILE} does not exist!"
  exit 1
fi

while IFS= read -r ip_host; do
  echo "Configuring ${ip_host}....."
  
  # Add verbose output for debugging
  sshpass -p "${SSH_PASSWORD}" ssh-copy-id -o HostKeyAlgorithms=+ssh-rsa -o PubkeyAcceptedKeyTypes=+ssh-rsa -o StrictHostKeyChecking=no -p ${SSH_PORT} root@"${ip_host}"

  if [[ $? -eq 0 ]]; then
    echo "Copy pub_key to ${ip_host} successful!"
  else
    echo "Copy pub_key to ${ip_host} failed!"
  fi
done < "${IPFILE}"